import request from '@/utils/request.js'
import { arrToTree } from '@/utils/tool.js'

// 加载字典
export function initDic(refDic) {
  let key = Object.keys(refDic.value)
  let pList = []

  key.forEach((item) => {
    if (!api[item]) {
      return
    }
    let p = api[item]().then((res) => {
      refDic.value[item] = []
      refDic.value[item] = res.data || res.rows
    })
    pList.push(p)
  })

  return Promise.all(pList)
}

export function updateDic(key, refDic, sendData = {}) {
  return api[key](sendData).then((res) => {
    // res.data.unshift({
    //   id: '',
    //   name: '全部',
    // })

    refDic.value[key] = []
    refDic.value[key] = res.data
  })
}

// 字典
const api = {
  // 数据库类型
  datasourceTypeDict(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/database/getDatasourceTypeDict',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          let ss = []
          for (let item of res.data) {
            ss.push({
              label: item,
              value: item,
            })
          }
          resolve({ data: ss })
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 数据模型 获取所有树节点列表
  // http://192.168.0.205:3000/project/151/interface/api/10315
  modelTreeAll(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/model/getTreeAllNodeList',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.data) {
            item.value = item.id
            item.label = item.name
          }

          let tree = arrToTree(res.data, {
            pid: 'parentId',
          })
          resolve({ data: tree })
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 数据模型 获取使用时候树节点列表
  // http://192.168.0.205:3000/project/151/interface/api/10323
  modelTreeEnable(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/model/getTreeUseNodeList',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.data) {
            item.value = item.id
            item.label = item.name
          }

          let tree = arrToTree(res.data, {
            pid: 'parentId',
          })
          resolve({ data: tree })
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 数据库列表
  // http://192.168.0.205:3000/project/151/interface/api/10227
  databaseList(sendData = {}) {
    sendData.pageSize = 100000
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/database/list',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.rows) {
            item.value = item.id
            item.label = item.datasourceName
          }

          resolve({ data: res.rows })
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 根据数据库 获取表格
  // http://192.168.0.205:3000/project/151/interface/api/10363
  databaseTable(datasourceId) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: `/datasource/getTables/${datasourceId}`,
        method: 'GET',
      }
      request
        .request(opt)
        .then((res) => {
          res.data = res.data || []
          const list = []
          for (let item of res.data) {
            list.push({
              value: item,
              label: item,
            })
          }

          resolve({ data: list, code: res.code, msg: res.msg })
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 根据数据库,表格 获取表字段
  // http://192.168.0.205:3000/project/151/interface/api/10371
  databaseColumn(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/datasource/getTableColumn',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          res.data = res.data || []
          const list = []
          for (let item of res.data) {
            list.push({
              value: item.name,
              label: item.name,
              type: item.type,
            })
          }

          resolve({ data: list })
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 字典列表
  // http://192.168.0.205:3000/project/151/interface/api/10467
  metaDict(sendData = {}) {
    return new Promise((resolve, reject) => {
      sendData.pageSize = 100000
      sendData.pageNum = 1
      const opt = {
        url: '/metaDict/tableList',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          res.rows = res.rows || []
          const list = []
          for (let item of res.rows) {
            list.push({
              value: `${item.id}`,
              label: item.name,
            })
          }

          resolve({ data: list })
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
}

export default api
export const $dic = api

// 位置信息
export function $locationArray(sendData) {
  return new Promise((resolve, reject) => {
    const opt = {
      url: '/base/location/array',
      method: 'GET',
      params: sendData,
    }
    request
      .request(opt)
      .then((res) => {
        resolve(res)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

export function $buildingTree(sendData) {
  return new Promise((resolve, reject) => {
    const opt = {
      url: '/base/location/array',
      method: 'GET',
      params: sendData,
    }
    request
      .request(opt)
      .then((res) => {
        resolve(res)
      })
      .catch((error) => {
        reject(error)
      })
  })
}
